package com.ruoyi.daily.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.daily.domain.dto.*;
import com.ruoyi.daily.mapper.QualityReportOneMapper;
import com.ruoyi.daily.service.QualityReportOneService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author cmz
 * @Date 2024-05-30 18:00
 */
@Service
public class QualityReportOneServiceImpl implements QualityReportOneService {
    @Resource
    private QualityReportOneMapper qualityReportOneMapper;
    @Override
    public R<TimelyCollectionOfYieldDto> timelyCollectionOfYield() {
        TimelyCollectionOfYieldDto timelyCollectionOfYieldVo = qualityReportOneMapper.timelyCollectionOfYield();
        return R.ok(timelyCollectionOfYieldVo);
    }

    @Override
    public R<DeliveryDelayDto> deliveryDelay() {
        DeliveryDelayDto deliveryDelayVo = qualityReportOneMapper.deliveryDelay();
        return R.ok(deliveryDelayVo);
    }

    @Override
    public R<FortyEightHRetentionDto> fortyEightHRetention() {
        FortyEightHRetentionDto fortyEightHRetentionVo = qualityReportOneMapper.fortyEightHRetention();
        return R.ok(fortyEightHRetentionVo);
    }

    @Override
    public R<SevenDaysFalseReceiptDto> falseReceipt() {
        SevenDaysFalseReceiptDto falseReceiptVo = qualityReportOneMapper.falseReceipt();
        return R.ok(falseReceiptVo);
    }

    @Override
    public R<List<SigningRateDto>> signingRate() {
        List<SigningRateDto> list = qualityReportOneMapper.signingRate();
        return R.ok(list);
    }

    @Override
    public R<SevenDayDeliveryDto> sevenDayDelivery() {
        SevenDayDeliveryDto dto = new SevenDayDeliveryDto();
        dto.setCollection(this.timelyCollectionOfYield().getData());
        dto.setDelayDto(this.deliveryDelay().getData());
        dto.setFortyEightHRetention(this.fortyEightHRetention().getData());
        dto.setEstimatedTotalPenalty(dto.getCollection().getFine()+dto.getDelayDto().getFine()+dto.getFortyEightHRetention().getFine());
        return R.ok(dto);
    }

    @Override
    public R<List<LastWeekShipmentVolumeDto>> lastWeekShipmentVolume() {
        List<LastWeekShipmentVolumeDto> list = new ArrayList<>();

        
        LastWeekShipmentVolumeDto l1 = new LastWeekShipmentVolumeDto();
        l1.setLable("盘锦公司");
        l1.setQuantity(30.00);
        l1.setOverallTaskIndicators(28.00);
        l1.setDailyAverageIndex(8.00);
        l1.setQuantityEnd(18.00);
        list.add(l1);

        LastWeekShipmentVolumeDto l2 = new LastWeekShipmentVolumeDto();
        l2.setLable("辽宁盘锦开发区公司");
        l2.setQuantity(32.00);
        l2.setOverallTaskIndicators(30.00);
        l2.setDailyAverageIndex(11.00);
        l2.setQuantityEnd(20.00);
        list.add(l2);

        LastWeekShipmentVolumeDto l3 = new LastWeekShipmentVolumeDto();
        l3.setLable("辽宁盘锦田家公司");
        l3.setQuantity(34.00);
        l3.setOverallTaskIndicators(32.00);
        l3.setDailyAverageIndex(13.00);
        l3.setQuantityEnd(22.00);
        list.add(l3);

        LastWeekShipmentVolumeDto l4 = new LastWeekShipmentVolumeDto();
        l4.setLable("辽宁盘锦田家公司");
        l4.setQuantity(34.00);
        l4.setOverallTaskIndicators(33.00);
        l4.setDailyAverageIndex(12.00);
        l4.setQuantityEnd(21.00);
        list.add(l4);

        LastWeekShipmentVolumeDto l5 = new LastWeekShipmentVolumeDto();
        l5.setLable("辽宁盘锦油田公司");
        l5.setQuantity(38.00);
        l5.setOverallTaskIndicators(33.00);
        l5.setDailyAverageIndex(22.00);
        l5.setQuantityEnd(24.00);
        list.add(l5);

        LastWeekShipmentVolumeDto l6 = new LastWeekShipmentVolumeDto();
        l6.setLable("辽宁大洼公司");
        l6.setQuantity(38.00);
        l6.setOverallTaskIndicators(33.00);
        l6.setDailyAverageIndex(22.00);
        l6.setQuantityEnd(24.00);
        list.add(l6);

        LastWeekShipmentVolumeDto l7 = new LastWeekShipmentVolumeDto();
        l7.setLable("辽宁盘锦双台子公司");
        l7.setQuantity(38.00);
        l7.setOverallTaskIndicators(33.00);
        l7.setDailyAverageIndex(22.00);
        l7.setQuantityEnd(24.00);
        list.add(l6);

        LastWeekShipmentVolumeDto l8 = new LastWeekShipmentVolumeDto();
        l8.setLable("辽宁盘锦盘山县公司");
        l8.setQuantity(38.00);
        l8.setOverallTaskIndicators(33.00);
        l8.setDailyAverageIndex(22.00);
        l8.setQuantityEnd(24.00);
        list.add(l8);
        
        return R.ok(list);
    }

    @Override
    public R<Map<String, Object>> returnBarLineEcharts() {
        List<LastWeekShipmentVolumeDto> data = this.lastWeekShipmentVolume().getData();
        Map<String, Object> arrayDto = new HashMap<>();

        List<String> lableList = new ArrayList<>();
        List<Double> quantityList = new ArrayList<>();
        List<Double> overallTaskIndicatorsList = new ArrayList<>();
        List<Double> dailyAverageIndexList = new ArrayList<>();
        List<Double> quantityEndList = new ArrayList<>();
        for (LastWeekShipmentVolumeDto datum : data) {
            lableList.add(datum.getLable());
            quantityList.add(datum.getQuantity());
            overallTaskIndicatorsList.add(datum.getOverallTaskIndicators());
            dailyAverageIndexList.add(datum.getDailyAverageIndex());
            quantityEndList.add(datum.getQuantityEnd());
        }

        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","止损指标");
        nameTimelinessRate.put("values",quantityList);
        nameTimelinessRate.put("type","line");
        nameTimelinessRate.put("yAxisIndex",1);
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","日均指标");
        nameTimelinessRate.put("values",dailyAverageIndexList);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","总任务指标");
        nameTimelinessRate.put("values",overallTaskIndicatorsList);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","止损指标");
        nameTimelinessRate.put("values",quantityEndList);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar1");
        list.add(nameTimelinessRate);

        List<Map<String,Object>> yAxis = new ArrayList<>();
        HashMap<String, Object> yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","单位:万");
        HashMap<String, Object> axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value}");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);

        yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","单位:KG");
        axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value} %");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);

        arrayDto.put("dateStrs",lableList);
        arrayDto.put("nameAndListValueDtos",list);
        arrayDto.put("yAxis",yAxis);
        
        return R.ok(arrayDto);
    }

    @Override
    public R<Map<String, Object>> signingRateReturnBarLineEcharts() {
        List<SigningRateDto> dtos = this.signingRate().getData();
        Map<String, Object> arrayDto = new HashMap<>();
        
        List<String> shouldOrgName = new ArrayList<>();
        List<Double> thisWeekSigningRate = new ArrayList<>();
        List<Double> lastWeekSigningRate = new ArrayList<>();
        List<Integer> firstStageEvaCount = new ArrayList<>();
        List<Integer> firstStageCount = new ArrayList<>();
        List<Integer> firstStageUntimelyCount = new ArrayList<>();
        List<Integer> secondStageEvaCount = new ArrayList<>();
        List<Integer> secondStageCount = new ArrayList<>();
        List<Integer> secondStageUntimelyCount = new ArrayList<>();

        for (SigningRateDto dto : dtos) {
            shouldOrgName.add(dto.getShouldOrgName());
            thisWeekSigningRate.add(dto.getThisWeekSigningRate());
            lastWeekSigningRate.add(dto.getLastWeekSigningRate());
            firstStageEvaCount.add(dto.getFirstStageEvaCount());
            firstStageCount.add(dto.getFirstStageCount());
            firstStageUntimelyCount.add(dto.getFirstStageUntimelyCount());
            secondStageEvaCount.add(dto.getSecondStageEvaCount());
            secondStageCount.add(dto.getSecondStageCount());
            secondStageUntimelyCount.add(dto.getSecondStageUntimelyCount());
        }
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","本周签收率");
        nameTimelinessRate.put("values",thisWeekSigningRate);
        nameTimelinessRate.put("type","line");
        nameTimelinessRate.put("yAxisIndex",1);
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","上周签收率");
        nameTimelinessRate.put("values",lastWeekSigningRate);
        nameTimelinessRate.put("type","line");
        nameTimelinessRate.put("yAxisIndex",2);
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","一阶段应收数量");
        nameTimelinessRate.put("values",firstStageEvaCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","一阶段及时签收数量");
        nameTimelinessRate.put("values",firstStageCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar1");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","一阶段未及时签收数量");
        nameTimelinessRate.put("values",firstStageUntimelyCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar1");
        list.add(nameTimelinessRate);


        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","二阶段应收数量");
        nameTimelinessRate.put("values",secondStageEvaCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","二阶段及时签收数量");
        nameTimelinessRate.put("values",secondStageCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar1");
        list.add(nameTimelinessRate);

        nameTimelinessRate = new HashMap<>();
        nameTimelinessRate.put("name","二阶段未及时签收数量");
        nameTimelinessRate.put("values",secondStageUntimelyCount);
        nameTimelinessRate.put("type","bar");
        nameTimelinessRate.put("stack","bar1");
        list.add(nameTimelinessRate);

        List<Map<String,Object>> yAxis = new ArrayList<>();
        HashMap<String, Object> yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","单位:万件");
        HashMap<String, Object> axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value}");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);

        yAxisMap1 = new HashMap<>();
        yAxisMap1.put("type","value");
        yAxisMap1.put("name","单位:%");
        axisLabel = new HashMap<>();
        axisLabel.put("formatter","{value} %");
        yAxisMap1.put("axisLabel",axisLabel);
        yAxis.add(yAxisMap1);

        arrayDto.put("dateStrs",shouldOrgName);
        arrayDto.put("nameAndListValueDtos",list);
        arrayDto.put("yAxis",yAxis);
        return R.ok(arrayDto);
    }

}
